from PyQt4 import QtGui
from PyQt4.QtCore import Qt

class Facebox(QtGui.QWidget):
        def __init__(self, *args, **kw):
		QtGui.QWidget.__init__(self, *args, **kw)
		self.setGeometry(0, 0, 187, 187)
#
#		facebox is made up of several layers
#

		bg = QtGui.QLabel(self) 
		bg.setGeometry(8, 8, 171, 171)
		path = "resources/facebox_back.png"
		bg.setPixmap(QtGui.QPixmap(path))

		face = QtGui.QLabel(self) 
		face.setGeometry(20, 20, 146, 146)
		face.hide();
		self.face = face

		temp = QtGui.QPixmap("resources/facebox_mask.png")
		self.mask = temp.mask()

		self.scanlines = QtGui.QLabel(self) 
		self.scanlines.setGeometry(10, 10, 167, 167)
		path = "resources/facebox_scanlines.png"
		self.scanlines.setPixmap(QtGui.QPixmap(path))
		self.scanlines.hide();


		frame = QtGui.QLabel(self) 
		frame.setGeometry(0, 0, 187, 187)
		path = "resources/facebox_frame.png"
		frame.setPixmap(QtGui.QPixmap(path))

	def set_face(self, path):
#
#		make a pixmap
#
		print "setting ", path
		pm = QtGui.QPixmap(path)

#
#		scale it to fit
#
		spm = pm.scaled(
			self.face.size(),
			Qt.KeepAspectRatioByExpanding
		)
		self.face.setPixmap(spm)
		self.face.setAlignment(Qt.AlignVCenter | Qt.AlignHCenter)
		self.face.setMask(self.mask)

		self.face.show()
		self.scanlines.show()

if __name__ == "__main__":
	import sys
	from glob import glob

	app = QtGui.QApplication(sys.argv)

	top = QtGui.QLabel()
	top.setGeometry(0, 0, 1024, 768)
	path = "resources/wood.jpg"
	top.setPixmap(QtGui.QPixmap(path))

	index = 0
	patt = "resources/characters/Scarlet Witch/Pr*jpg"
	pics = glob(patt)
	screen = Facebox(top)
	screen.move(0,0)
	screen.set_face(pics[index])

	def next_cb():
		global index

		index += 1
		if index >= len(pics):
			index = 0
		screen.set_face(pics[index])

	next = QtGui.QPushButton(top)
	next.setText("Next")
	next.clicked.connect(next_cb)
	next.move(50,400)


	top.show()
	sys.exit(app.exec_())


